﻿

Module XMI

    Public Function getXMIEnveloppe() As Xml.XmlDocument
        Dim XMIEnveloppe As Xml.XmlDocument = New Xml.XmlDocument

        XMIEnveloppe.Load("Enveloppe.xml")


        Return XMIEnveloppe
    End Function



    Public Function MakeXMI_UML_Class(ByVal ClassNumber As Integer, _
                                      ByVal ItemTypeName As String, _
                                      ByVal ModelNumber As Integer, _
                                      ByVal SubClassString As String, _
                                      ByVal ConnectionString As String, _
                                      ByVal IncludeAttributes As String, _
                                      Optional ByRef TabProperty As String() = Nothing) As String

        'make XMI-UML class string
        Dim UMLClass As String
        Dim i As Integer = 0

        UMLClass = "<UML:Class xmlns:UML='href://org.omg/UML/1.3' "
        UMLClass += "xmi.id='UMLClass." & CStr(ClassNumber) & "' "
        UMLClass += "name='" & ItemTypeName & "' "
        UMLClass += "visibility='public' "
        UMLClass += "isSpecification='false' "
        UMLClass += "namespace='UMLModel." & CStr(ModelNumber) & "' "
        UMLClass += "isRoot = 'false' "
        UMLClass += "isLeaf = 'false' "
        UMLClass += "isAbstract = 'false' "
        UMLClass += "specialization = '" & SubClassString & "' "
        UMLClass += "participant = '" & ConnectionString & "' "
        If IncludeAttributes = "none" Then
            UMLClass += "isActive='false'/>"
        ElseIf IncludeAttributes = "all" Then
            UMLClass += "isActive='false'>"
            For i = 0 To TabProperty.Length - 1
                UMLClass += "<UML:Attribute "
                UMLClass += "xmi.id='UMLAttribute." & CStr(ClassNumber) & "." & i & "' "
                UMLClass += "name='" & TabProperty(i) & "' "
                UMLClass += "visibility='public' "
                UMLClass += "isSpecification='false' "
                UMLClass += "ownerScope='instance' "
                UMLClass += "changeability='changeable' "
                UMLClass += "targetScope='instance' "
                UMLClass += "type='' "
                UMLClass += "owner='UMLClass." & CStr(ClassNumber) & "'/>"

            Next
            UMLClass += "</UML:Class>"
        Else
            UMLClass += "isActive='false'>"
            For i = 0 To TabProperty.Length
                UMLClass += "<UML:Attribute "
                UMLClass += "xmi.id='UMLAttribute." & CStr(ClassNumber) & "." & i & "' "
                UMLClass += "name='" & TabProperty(i) & "' "
                UMLClass += "visibility='public' "
                UMLClass += "isSpecification='false' "
                UMLClass += "ownerScope='instance' "
                UMLClass += "changeability='changeable' "
                UMLClass += "targetScope='instance' "
                UMLClass += "type='' "
                UMLClass += "owner='UMLClass." & CStr(ClassNumber) & "'/>"

            Next
            UMLClass += "</UML:Class>"
        End If

        Return UMLClass

    End Function

    Public Function getXMIClassNumber(ByRef XMIEnveloppe As Xml.XmlDocument, _
                                      ByVal ClassName As String) As Integer

        Dim UMLClassNumber As Integer
        Dim NodeList As Xml.XmlNodeList
        Dim i As Integer

        NodeList = XMIEnveloppe.GetElementsByTagName("UML:Class")
        For i = 0 To NodeList.Count - 1
            If NodeList.Item(i).Attributes("name").Value = ClassName Then
                UMLClassNumber = CInt(Split((NodeList.Item(i).Attributes("xmi.id").Value), ".")(1))
                GoTo EndFunction
            End If
        Next

EndFunction:

        Return UMLClassNumber
    End Function


    Public Function getXMIAssociationClassNumber(ByRef XMIEnveloppe As Xml.XmlDocument, _
                                  ByVal ClassName As String) As Integer

        Dim UMLClassNumber As Integer
        Dim NodeList As Xml.XmlNodeList
        Dim i As Integer

        NodeList = XMIEnveloppe.GetElementsByTagName("UML:AssociationClass")
        For i = 0 To NodeList.Count - 1
            If NodeList.Item(i).Attributes("name").Value = ClassName Then
                UMLClassNumber = CInt(Split((NodeList.Item(i).Attributes("xmi.id").Value), ".")(1))
                GoTo EndFunction
            End If
        Next

EndFunction:

        Return UMLClassNumber
    End Function




    Public Function MakeXMI_UML_SubClass(ByVal ClassNumber As Integer, _
                                      ByVal ItemTypeName As String, _
                                      ByVal ModelNumber As Integer, _
                                      ByVal GeneralizationNumber As Integer) As String

        'make XMI-UML class string
        Dim SubClass As String

        SubClass = "<UML:Class xmlns:UML='href://org.omg/UML/1.3' "
        SubClass += "xmi.id='UMLClass." & CStr(ClassNumber) & "' "
        SubClass += "name='" & ItemTypeName & "' "
        SubClass += "visibility='public' "
        SubClass += "isSpecification='false' "
        SubClass += "namespace='UMLModel." & CStr(ModelNumber) & "' "
        SubClass += "isRoot = 'false' "
        SubClass += "isLeaf = 'false' "
        SubClass += "isAbstract = 'false' "
        SubClass += "generalization = 'UMLGeneralization." & CStr(GeneralizationNumber) & "' "
        SubClass += "isActive='false'/>"

        Return SubClass

    End Function

    Public Function MakeXMI_UML_Generalization(ByVal GeneralizationNumber As Integer, _
                                               ByVal ModelNumber As Integer, _
                                               ByVal ChildReference As Integer, _
                                               ByVal ParentReference As Integer) As String

        'make XMI-UML class string
        Dim UMLGeneralization As String

        UMLGeneralization = "<UML:Generalization xmlns:UML='href://org.omg/UML/1.3' "
        UMLGeneralization += "xmi.id='UMLGeneralization." & CStr(GeneralizationNumber) & "' "
        UMLGeneralization += "name='' "
        UMLGeneralization += "visibility='public' "
        UMLGeneralization += "isSpecification='false' "
        UMLGeneralization += "namespace='UMLModel." & CStr(ModelNumber) & "' "
        UMLGeneralization += "discriminator = '""' "
        UMLGeneralization += "isLeaf = 'false' "
        UMLGeneralization += "isAbstract = 'false' "
        UMLGeneralization += "child = 'UMLClass." & CStr(ChildReference) & "' "
        UMLGeneralization += "parent = 'UMLClass." & CStr(ParentReference) & "'/>"

        Return UMLGeneralization

    End Function

    Public Function MakeXMI_UML_DirectedOneToOneConnection(ByVal DirectedOneToOneNumber As Integer, _
                                                           ByVal ModelNumber As Integer, _
                                                           ByVal ChildClassNumber As Integer, _
                                                           ByVal ParentClassNumber As Integer, _
                                                           ByVal ParentpropertyName As String, _
                                                           ByVal MultiplicityNumber As Integer, _
                                                           ByVal parent_is_relationship As Boolean, _
                                                           ByVal PropertyName As String) As String


        'make XMI-UML class string
        Dim DirectedOneToOne As String

        DirectedOneToOne = "<UML:Association xmlns:UML='href://org.omg/UML/1.3' "
        DirectedOneToOne += "xmi.id='UMLAssociation." & CStr(DirectedOneToOneNumber) & "' "
        DirectedOneToOne += "visibility='public' "
        DirectedOneToOne += "name='" & PropertyName & "' "
        DirectedOneToOne += "isSpecification='false' "
        DirectedOneToOne += "namespace='UMLModel." & CStr(ModelNumber) & "'> "

        DirectedOneToOne += "<UML:Association.connection>"

        DirectedOneToOne += "<UML:AssociationEnd "
        DirectedOneToOne += "xmi.id='UMLAssociationEnd." & CStr(DirectedOneToOneNumber + 1) & "' "
        DirectedOneToOne += "name='' "
        DirectedOneToOne += "visibility='public' "
        DirectedOneToOne += "isSpecification='false' "
        DirectedOneToOne += "isNavigable='false' "
        DirectedOneToOne += "ordering='unordered' "
        DirectedOneToOne += "aggregation='none' "
        DirectedOneToOne += "targetScope='instance' "
        DirectedOneToOne += "changeability='changeable' "
        DirectedOneToOne += "association='UMLAssociationClass." & CStr(DirectedOneToOneNumber) & "' "
        DirectedOneToOne += "type='UMLClass." & ChildClassNumber & "'> "

        DirectedOneToOne += "<UML:AssociationEnd.multiplicity>"
        DirectedOneToOne += "<UML:Multiplicity xmi.id='X." & MultiplicityNumber & "'>"
        DirectedOneToOne += "<UML:Multiplicity.range>"
        DirectedOneToOne += "<UML:MultiplicityRange xmi.id='X." & MultiplicityNumber + 1 & "' lower='1' upper='N' multiplicity='X." & MultiplicityNumber & "'/>"
        DirectedOneToOne += "</UML:Multiplicity.range>"
        DirectedOneToOne += "</UML:Multiplicity>"
        DirectedOneToOne += "</UML:AssociationEnd.multiplicity>"
        DirectedOneToOne += "</UML:AssociationEnd>"

        DirectedOneToOne += "<UML:AssociationEnd "
        DirectedOneToOne += "xmi.id='UMLAssociationEnd." & CStr(DirectedOneToOneNumber + 2) & "' "
        DirectedOneToOne += "name='' "
        DirectedOneToOne += "visibility='public' "
        DirectedOneToOne += "isSpecification='false' "
        DirectedOneToOne += "isNavigable='true' "
        DirectedOneToOne += "ordering='unordered' "
        DirectedOneToOne += "aggregation='none' "
        DirectedOneToOne += "targetScope='instance' "
        DirectedOneToOne += "changeability='changeable' "
        DirectedOneToOne += "association='UMLAssociationClass." & CStr(DirectedOneToOneNumber) & "' "

        If parent_is_relationship = True Then
            DirectedOneToOne += "type='UMLAssociationClass." & ParentClassNumber & "' > "
        Else
            DirectedOneToOne += "type='UMLClass." & ParentClassNumber & "' > "
        End If

        DirectedOneToOne += "<UML:AssociationEnd.multiplicity>"
        DirectedOneToOne += "<UML:Multiplicity xmi.id='X." & MultiplicityNumber + 2 & "'>"
        DirectedOneToOne += "<UML:Multiplicity.range>"
        DirectedOneToOne += "<UML:MultiplicityRange xmi.id='X." & MultiplicityNumber + 3 & "' lower='1' upper='1' multiplicity='X." & MultiplicityNumber + 2 & "'/>"
        DirectedOneToOne += "</UML:Multiplicity.range>"
        DirectedOneToOne += "</UML:Multiplicity>"
        DirectedOneToOne += "</UML:AssociationEnd.multiplicity>"
        DirectedOneToOne += "</UML:AssociationEnd>"

        DirectedOneToOne += "</UML:Association.connection>"
        DirectedOneToOne += "</UML:Association>"

        Return DirectedOneToOne


    End Function

    Public Function MakeXMI_UML_AssociationClass(ByVal AssociationClassNumber As Integer, _
                                                 ByVal RelationshipName As String, _
                                                 ByVal ModelNumber As Integer, _
                                                 ByVal ChildClassNumber As Integer, _
                                                 ByVal ParentClassNumber As Integer, _
                                                 ByVal parent_is_relationship As Boolean, _
                                                 ByVal child_is_relationship As Boolean, _
                                                 ByVal IncludeAttributes As String, _
                                                 Optional ByRef TabProperty As String() = Nothing) As String
        'debug
        If AssociationClassNumber = 220 Then
            'MsgBox("t")
        End If
        'debug
        'make XMI-UML class string
        Dim AssociationClass As String

        AssociationClass = "<UML:AssociationClass xmlns:UML='href://org.omg/UML/1.3' "
        AssociationClass += "xmi.id='UMLAssociationClass." & CStr(AssociationClassNumber) & "' "
        AssociationClass += "name='" & RelationshipName & "' "
        AssociationClass += "visibility='public' "
        AssociationClass += "isSpecification='false' "
        AssociationClass += "namespace='UMLModel." & CStr(ModelNumber) & "' "
        AssociationClass += "isRoot = 'false' "
        AssociationClass += "isLeaf = 'false' "
        AssociationClass += "isAbstract = 'false' "
        AssociationClass += "isActive='false'>"
        AssociationClass += "<UML:Association.connection>"
        AssociationClass += "<UML:AssociationEnd "
        AssociationClass += "xmi.id='UMLAssociationEnd." & CStr(AssociationClassNumber + 1) & "' "
        AssociationClass += "name='' "
        AssociationClass += "visibility='public' "
        AssociationClass += "isSpecification='false' "
        AssociationClass += "isNavigable='true' "
        AssociationClass += "ordering='unordered' "
        AssociationClass += "aggregation='aggregate' "
        AssociationClass += "targetScope='instance' "
        AssociationClass += "changeability='changeable' "
        AssociationClass += "association='UMLAssociationClass." & CStr(AssociationClassNumber) & "' "

        If parent_is_relationship = True Then
            AssociationClass += "type='UMLAssociationClass." & ParentClassNumber & "' /> "
        Else
            AssociationClass += "type='UMLClass." & ParentClassNumber & "' /> "
        End If

        AssociationClass += "<UML:AssociationEnd "
        AssociationClass += "xmi.id='UMLAssociationEnd." & CStr(AssociationClassNumber + 2) & "' "
        AssociationClass += "name='' "
        AssociationClass += "visibility='public' "
        AssociationClass += "isSpecification='false' "
        AssociationClass += "isNavigable='true' "
        AssociationClass += "ordering='unordered' "
        AssociationClass += "aggregation='none' "
        AssociationClass += "targetScope='instance' "
        AssociationClass += "changeability='changeable' "
        AssociationClass += "association='UMLAssociationClass." & CStr(AssociationClassNumber) & "' "
        If child_is_relationship = True Then
            AssociationClass += "type='UMLAssociationClass." & ChildClassNumber & "' /> "
        Else
            AssociationClass += "type='UMLClass." & ChildClassNumber & "' /> "
        End If
        AssociationClass += "</UML:Association.connection>"


        'Adding Attributes if asked
        If IncludeAttributes = "none" Then
        ElseIf IncludeAttributes = "all" Then
            AssociationClass += "isActive='false'>"
            For i = 0 To TabProperty.Length - 1
                AssociationClass += "<UML:Attribute "
                AssociationClass += "xmi.id='UMLAttribute." & CStr(AssociationClassNumber) & "." & i & "' "
                AssociationClass += "name='" & TabProperty(i) & "' "
                AssociationClass += "visibility='public' "
                AssociationClass += "isSpecification='false' "
                AssociationClass += "ownerScope='instance' "
                AssociationClass += "changeability='changeable' "
                AssociationClass += "targetScope='instance' "
                AssociationClass += "type='' "
                AssociationClass += "owner='UMLAssociationClass." & CStr(AssociationClassNumber) & "'/>"

            Next
        Else
            AssociationClass += "isActive='false'>"
            For i = 0 To TabProperty.Length
                AssociationClass += "<UML:Attribute "
                AssociationClass += "xmi.id='UMLAttribute." & CStr(AssociationClassNumber) & "." & i & "' "
                AssociationClass += "name='" & TabProperty(i) & "' "
                AssociationClass += "visibility='public' "
                AssociationClass += "isSpecification='false' "
                AssociationClass += "ownerScope='instance' "
                AssociationClass += "changeability='changeable' "
                AssociationClass += "targetScope='instance' "
                AssociationClass += "type='' "
                AssociationClass += "owner='UMLAssociationClass." & CStr(AssociationClassNumber) & "'/>"

            Next

        End If



        AssociationClass += "</UML:AssociationClass>"

        Return AssociationClass


    End Function


    Public Function MakeXMI_UML_Association(ByVal AssociationClassNumber As Integer, _
                                            ByVal ModelNumber As Integer, _
                                            ByVal ChildClassNumber As Integer, _
                                            ByVal ParentClassNumber As Integer, _
                                            ByVal parent_is_relationship As Boolean) As String
        Dim Association As String



        Association = "<UML:Association xmlns:UML='href://org.omg/UML/1.3' "
        Association += "xmi.id='UMLAssociation." & CStr(AssociationClassNumber) & "' "
        Association += "visibility='public' "
        Association += "isSpecification='false' "
        Association += "namespace='UMLModel." & CStr(ModelNumber) & "'> "
        Association += "<UML:Association.connection>"
        Association += "<UML:AssociationEnd "
        Association += "xmi.id='UMLAssociationEnd." & CStr(AssociationClassNumber + 1) & "' "
        Association += "name='' "
        Association += "visibility='public' "
        Association += "isSpecification='false' "
        Association += "isNavigable='true' "
        Association += "ordering='unordered' "
        Association += "aggregation='aggregate' "
        Association += "targetScope='instance' "
        Association += "changeability='changeable' "
        Association += "association='UMLAssociationClass." & CStr(AssociationClassNumber) & "' "
        Association += "type='UMLClass." & ChildClassNumber & "' /> "
        Association += "<UML:AssociationEnd "
        Association += "xmi.id='UMLAssociationEnd." & CStr(AssociationClassNumber + 2) & "' "
        Association += "name='' "
        Association += "visibility='public' "
        Association += "isSpecification='false' "
        Association += "isNavigable='true' "
        Association += "ordering='unordered' "
        Association += "aggregation='none' "
        Association += "targetScope='instance' "
        Association += "changeability='changeable' "
        Association += "association='UMLAssociationClass." & CStr(AssociationClassNumber) & "' "
        If parent_is_relationship = True Then
            Association += "type='UMLAssociationClass." & ParentClassNumber & "' /> "
        Else
            Association += "type='UMLClass." & ParentClassNumber & "' /> "
        End If
        Association += "type='UMLClass." & ParentClassNumber & "' /> "
        Association += "</UML:Association.connection>"
        Association += "</UML:Association>"

        Return Association
    End Function


    Public Function AddNodeToTemplate(ByVal UMLClass As String, ByVal XMIenvelop As Xml.XmlDocument) As Xml.XmlDocument

        Dim ItemTypesN As Xml.XmlDocumentFragment

        ' Ajout de l'élément créé à l'enveloppe
        ItemTypesN = XMIenvelop.CreateDocumentFragment
        ItemTypesN.InnerXml = UMLClass
        XMIenvelop.GetElementsByTagName("UML:Namespace.ownedElement").Item(0) _
                                .ChildNodes.Item(2).ChildNodes.Item(0).AppendChild(ItemTypesN)

        Return XMIenvelop

    End Function
End Module
